import io.vertx.core.AbstractVerticle
import io.vertx.core.Vertx
import io.vertx.core.parsetools.RecordParser

/**
 * Created by Lzx on 2017/12/7.
 */
class Server extends AbstractVerticle {

    static void main(def args) {
        Vertx.vertx().deployVerticle(Server.newInstance())
    }

    @Override
    void start() throws Exception {
        def netServer = vertx.createNetServer()

        netServer.connectHandler({ sock ->
            RecordParser parser = RecordParser.newDelimited("\n", sock)

            parser.endHandler({v -> sock.close()})
                    .exceptionHandler({ t->
                t.printStackTrace()
                sock.close()
            }).handler({buffer ->
                String name = buffer.toString("UTF-8")
                sock.write("Hello " + name + "\n", "UTF-8")
            })

        }).listen(1234)
    }
}
